//
//  ViewController.m
//  TTKit
//
//  Created by Apple on 2017/11/13.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "ViewController.h"
#import "APIKit.h"
#import "UIKit+AFNetworking.h"

#import "UIKit+TTKit.h"


#define WXCellWidth  315/2
static NSString *ReuseIdentifier = @"YTReaderViewController";

@interface ViewController()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (strong,nonatomic) UICollectionView           *collectView;
@property (nonatomic) int icount;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    NSLog(@"Begin request");
    
    self.icount = 9;
    /*TTRequest *request = [[TTRequest alloc]init];
     [request asynRequestServerAndParser:@"http://itunes.apple.com/lookup?id=670670008"
                            withPostData:nil
                         withParserModal:[[appinfoModel alloc]init]
                             withSuccess:^(TTInterfaceModel *model) {
        NSLog(@"Success");
        appinfoModel *infoModel = (appinfoModel*)model;
        [self showMessageBoxOnWindow:((appinfoItemModel*)infoModel.results[0]).artworkUrl60
        mode:MBProgressHUDModeText];
     }withFail:^(NSString *error) {
        NSLog(@"Fail");
     }];
     UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]init];
     [rightItem setTitle:@"sdfjs"];
     self.navigationItem.rightBarButtonItem = rightItem;
     self.title = @"测试";
     [self.navigationController.navigationItem.rightBarButtonItem setTitle:@"good"];*/
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    // 1.设置列间距
    layout.minimumInteritemSpacing = 1;
    // 2.设置行间距
    layout.minimumLineSpacing = 1;
    
    self.collectView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,0, self.view.width,
                                                                         self.view.height - self.tabBarController.tabBar.frame.size.height  /*- self.navigationController.navigationBar.frame.size.height - [UIApplication sharedApplication].statusBarFrame.size.height*/)
                                         collectionViewLayout:layout];
    [self.collectView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ReuseIdentifier];
    self.collectView.backgroundColor = [UIColor blackColor];
    self.collectView.delegate = self;
    self.collectView.dataSource = self;
    self.collectView.bounces = YES;
    self.collectView.alwaysBounceVertical = YES;
    self.collectView.alwaysBounceHorizontal = NO;
    [self.view addSubview:self.collectView];
    
    // The drop-down refresh
    self.collectView.mj_header = [TTRefreshHeader headerWithRefreshingBlock:^{
        //Call this Block When enter the refresh status automatically
        [self.collectView.mj_header endRefreshing];
    }];
    
    // The pull to refresh
    self.collectView.mj_footer = [TTRefreshFooter footerWithRefreshingBlock:^{
        //Call this Block When enter the refresh status automatically
        self.collectView.alpha = 1.0f;
        [UIView animateWithDuration:2.0f animations:^{
            self.collectView.alpha = 0.5f;
        } completion:^(BOOL finished) {
            self.collectView.alpha = 1.0f;
            self.icount += 6;
            [self.collectView reloadData];
            [self.collectView.mj_footer endRefreshing];
        }];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
    
}


#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.icount;
}

//初始化展示的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ReuseIdentifier
                                                                           forIndexPath:indexPath];
    //NSArray *itemArray = @[@"name1",@"name2",@"name3",@"name3"];

    if(cell.subviews.count < 2){
        UIImageView *imgView = [[UIImageView alloc]init];
        imgView.contentMode =  UIViewContentModeTop;
        imgView.clipsToBounds = YES;
        UIImage *defImg = [self imageWithColor:[UIColor grayColor] size:CGSizeMake(cell.width,cell.height)];
        NSURL *url = [NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513964215571&di=c60898538ac6524f39f55839da9e7371&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2Fe1fe9925bc315c600dce09d386b1cb13495477b6.jpg"];//http://img2.winxuancdn.com/9922/1201589922_9.jpg?1505725875938"];
        __weak typeof(UIImageView*)weakSelf = imgView;
        [imgView setImageWithURLRequest:[NSURLRequest requestWithURL:url]
                                                    placeholderImage:defImg
                                                             success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull image) {
            [weakSelf setImage:image];
        } failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
            [weakSelf setImage:defImg];
        }];
        imgView.frame = CGRectMake(0,0,cell.width-20,cell.height-10);
        imgView.center = CGPointMake(cell.width/2, cell.height/2);
        
        UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,imgView.tail+33/2,cell.width,14)];
        //textLabel.text = self.dataArray[indexPath.item];
        textLabel.text = [NSString stringWithFormat:@"name%ld",indexPath.item];//[itemArray objectAtIndex:indexPath.item];
        textLabel.font = [UIFont systemFontOfSize:14];
        textLabel.textColor = RGBCOLOR(51, 51, 51, 1);
        textLabel.textAlignment = NSTextAlignmentCenter;
        textLabel.backgroundColor = [UIColor clearColor];
        textLabel.lineBreakMode = NSLineBreakByTruncatingTail | NSLineBreakByWordWrapping;
        [cell addSubview:imgView];
        [cell addSubview:textLabel];
        
        cell.layer.masksToBounds = YES;
        cell.layer.cornerRadius = 3.0f;
        //cell.layer.borderColor = [RGBCOLOR(203, 204, 205, 1) CGColor];
        //cell.layer.borderWidth = 1.0f;
        cell.backgroundColor = [UIColor grayColor];
    }
    return cell;
}

//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(2,2,2,2);
}

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{    
    return CGSizeMake((DEVICE_WIDTH-2*4-2)/4,(DEVICE_WIDTH-2*4-2)/3);
}
#pragma mark <UICollectionViewDelegate>
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self showMessageBoxOnWindow:[NSString stringWithFormat:@"indexPath.item:%ld",indexPath.item] mode:MBProgressHUDModeText];
}
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

//UICollectionView取消选中调用的方法
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    //cell.layer.borderColor = [RGBCOLOR(203, 204, 205, 1) CGColor];
    //cell.layer.borderWidth = 1.0f;
}


@end

